package com.happy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.dao.UserMapper;
import com.happy.entity.UserEntity;
import com.happy.service.IUserService;
import com.happy.util.StateEnum;
import com.happy.util.StateException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Resource
    private UserMapper userMapper;


    @Override
    public UserEntity querySingle ( UserEntity userEntity ) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<> ();
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( userEntity.getId () != null ) {
            queryWrapper.eq ( "id", userEntity.getId () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getUserName () ) ) {
            queryWrapper.eq ( "userName", userEntity.getUserName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getPassword () ) ) {
            queryWrapper.eq ( "password", userEntity.getPassword () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getNickName () ) ) {
            queryWrapper.like ( "nickName", userEntity.getNickName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getPhone () ) ) {
            queryWrapper.eq ( "phone", userEntity.getPhone () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getEmail () ) ) {
            queryWrapper.eq ( "email", userEntity.getEmail () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getCompany () ) ) {
            queryWrapper.like ( "company", userEntity.getCompany () );
        }
        if ( userEntity.getSex () != null ) {
            queryWrapper.eq ( "sex", userEntity.getSex () );
        }
        userEntity = baseMapper.selectOne ( queryWrapper );
        return userEntity;
    }


    @Override
    public List<UserEntity> queryList ( UserEntity userEntity ) {
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        List<UserEntity> userEntities = userMapper.queryList ( ( userEntity.getPageNum () - 1 ) * userEntity.getPageSize (), userEntity.getPageSize () );
        return userEntities;
    }

}
